<?php

namespace app\modules\admin\controllers;

use app\modules\admin\models\form\AdminForm;
use Yii;
use yii\web\Controller;

/**
 * Default controller for the `admin` module
 */
class DefaultController extends Controller
{
	public function actions()
	{
		return [
			'error' => [
				'class' => 'yii\web\ErrorAction',
			],
			'captcha' => [
				'class' => 'yii\captcha\CaptchaAction',
				'height' => 34,
				'minLength' => 5,
				'maxLength' => 5,
//                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
			],
		];
	}

	/**
	 * Renders the index view for the module
	 * @return string
	 */
	public function actionIndex()
	{
		return $this->render('index');
	}

	public function actionLogin() {
		if(!Yii::$app->user->isGuest) {
			return $this->goHome();
		}

		$this->layout = false;

		$admin = new AdminForm();
		$admin->setScenario(AdminForm::SCENARIO_LOGIN);
		if($admin->load(Yii::$app->request->post()) && $admin->login()) {
			return $this->goHome();
		}

		return $this->render('login', ['model'=>$admin]);
	}
}
